package cn.wolfcode.car.business.service.impl;


import cn.wolfcode.car.base.domain.User;
import cn.wolfcode.car.business.domain.BpmnInfo;
import cn.wolfcode.car.business.domain.CarPackageAudit;
import cn.wolfcode.car.business.domain.ServiceItem;
import cn.wolfcode.car.business.domain.StatementItem;
import cn.wolfcode.car.business.mapper.BpmnInfoMapper;
import cn.wolfcode.car.business.mapper.CarPackageAuditMapper;
import cn.wolfcode.car.business.mapper.ServiceItemMapper;
import cn.wolfcode.car.business.query.ServiceItemQueryObject;
import cn.wolfcode.car.business.service.IServiceItemService;
import cn.wolfcode.car.common.base.page.TablePageInfo;
import cn.wolfcode.car.common.exception.BusinessException;
import cn.wolfcode.car.common.util.Convert;
import cn.wolfcode.car.shiro.ShiroUtils;
import com.github.pagehelper.PageHelper;
import org.activiti.engine.RuntimeService;
import org.activiti.engine.runtime.ProcessInstance;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
@Transactional
public class ServiceItemServiceImpl implements IServiceItemService {

    @Autowired
    private ServiceItemMapper serviceItemMapper;
    @Autowired
    private CarPackageAuditMapper carPackageAuditMapper;
    @Autowired
    private RuntimeService runtimeService;
    @Autowired
    private BpmnInfoMapper bpmnInfoMapper;




    @Override
    public TablePageInfo<ServiceItem> query(ServiceItemQueryObject qo) {
        PageHelper.startPage(qo.getPageNum(), qo.getPageSize());
        return new TablePageInfo<>(serviceItemMapper.selectForList(qo));
    }


    @Override
    public void save(ServiceItem serviceItem) {
        serviceItem.setCreateTime(new Date());
        Integer carPackage = serviceItem.getCarPackage();
        if (carPackage == ServiceItem.CARPACKAGE_YES) {
            serviceItem.setAuditStatus(ServiceItem.AUDITSTATUS_INIT);
        } else {
            serviceItem.setAuditStatus(ServiceItem.AUDITSTATUS_NO_REQUIRE);
        }
        serviceItemMapper.insert(serviceItem);
    }

    @Override
    public ServiceItem get(Long id) {
        return serviceItemMapper.selectByPrimaryKey(id);
    }


    @Override
    public void update(ServiceItem serviceItem) {
        serviceItemMapper.updateByPrimaryKey(serviceItem);
    }

    @Override
    public void deleteBatch(String ids) {
        Long[] dictIds = Convert.toLongArray(ids);
        for (Long dictId : dictIds) {
            serviceItemMapper.deleteByPrimaryKey(dictId);
        }
    }

    @Override
    public void saleOn(Long id) {
        ServiceItem serviceItem = serviceItemMapper.selectByPrimaryKey(id);
        if (serviceItem.getCarPackage() == ServiceItem.CARPACKAGE_YES &&
                serviceItem.getAuditStatus() != ServiceItem.AUDITSTATUS_APPROVED) {
            //不能上架
            throw new BusinessException("套餐需要审核才能上架");
        }
        //
        serviceItemMapper.changeSaleStatus(id, ServiceItem.SALESTATUS_ON);
    }

    @Override
    public void saleOff(Long id) {
        serviceItemMapper.changeSaleStatus(id, ServiceItem.SALESTATUS_OFF);
    }

    @Override
    public void startAudit(Long id, Long bpmnInfoId, Long director, Long finance, String info) {
        //
        //通过id查serviceItem
        ServiceItem serviceItem = serviceItemMapper.selectByPrimaryKey(id);
        //审批中,  修改service_item状态为审批中
        serviceItemMapper.changeAuditStatus(id, ServiceItem.AUDITSTATUS_AUDITING);
        //生成套餐审核表记录(bus_car_package_audit)
        CarPackageAudit audit = new CarPackageAudit();
        audit.setServiceItemId(serviceItem.getId());
        audit.setServiceItemInfo(serviceItem.getInfo());
        //审批使用的是打折金额
        audit.setServiceItemPrice(serviceItem.getDiscountPrice());
        //获取当前用户
        User user = ShiroUtils.getUser();
        audit.setCreator(user.getUserName());
        audit.setAuditorId(director);
        audit.setBpmninfoId(bpmnInfoId);
        audit.setInfo(info);
        audit.setCreateTime(new Date());
        //插入bus_car_package_audit记录生成主键id,  生成主键id
        carPackageAuditMapper.insert(audit);
        //activiti发起流程实例
        BpmnInfo bpmnInfo = bpmnInfoMapper.selectByPrimaryKey(bpmnInfoId);
        String processDefinationKey = bpmnInfo.getActProcessKey();
        String businessKey = audit.getId().toString();
        Map<String,Object> map = new HashMap();
        map.put("discountPrice", audit.getServiceItemPrice().longValue());
        map.put("director", director.toString());
        if (finance != null) {
            map.put("finance", finance.toString());
        }
        ProcessInstance processInstance = runtimeService.startProcessInstanceByKey(processDefinationKey, businessKey, map);
        audit.setInstanceId(processInstance.getId());
        carPackageAuditMapper.updateByPrimaryKey(audit);
    }

}
